package com.tanhua.dubbo.api;

import com.tanhua.model.domain.mongo.Comment;
import com.tanhua.model.domain.CommentType;
import com.tanhua.model.domain.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author lk
 * @date 2022-03-28
 */
@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //分页查询
    @Override
    public List<Comment> findComments(String movementId, Integer page, Integer pagesize, CommentType comment) {
        Query query = Query.query(Criteria
                        .where("publishId").is(new ObjectId(movementId))
                        .and("commentType").is(comment.getType()))
                .skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        //查询并返回
        return mongoTemplate.find(query, Comment.class);
    }

    //保存
    @Override
    public Integer save(Comment com) {
        //查询动态
        Movement movement = mongoTemplate.findById(com.getPublishId(), Movement.class);
        //向comment对象设置被评论人属性
        if (movement != null) {
            com.setPublishUserId(movement.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(com);
        //更新动态表中的对应字段
        Query query = Query.query(Criteria.where("id").is(com.getPublishId()));
        Update update = new Update();
        //判断本次操作是 评论/喜欢/点赞  将次数加一
        if (com.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (com.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //设置更新参数   FindAndModifyOptions 选择查询和调整
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //获取最新的评论数量，并返回
        return modify.statisCount(com.getCommentType());
    }

    //判断comment数据是否存在
    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType type) {
        Query query = Query.query(Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(type.getType()));
        return mongoTemplate.exists(query, Comment.class);
    }

    //删除互动数据
    @Override
    public Integer delete(Comment comment) {
        //删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);

        //修改动态表中的总数量
        Query movementQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        //判断是 点赞/喜欢/评论 并将数量减一
        if(comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(movementQuery,update,options,Movement.class);
        //获取最新的评论数量，并返回
        return modify.statisCount(comment.getCommentType());
    }

    //判断该评论是否已点赞
    @Override
    public Boolean findComment(String id, Long userId, CommentType like) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("commentType").is(like.getType()).and("userId").is(userId);
        Query query = Query.query(criteria);
        boolean exists = mongoTemplate.exists(query, Comment.class);
        return exists;
    }

    //评论点赞保存
    @Override
    public Integer saveComment(Comment com) {
        //查询评论
        Comment comment = mongoTemplate.findById(com.getPublishId(), Comment.class);
        //向comment对象设置被评论人属性
        if (comment != null) {
            com.setPublishUserId(comment.getUserId());
        }
        //保存到数据库
        mongoTemplate.save(com);
        //更新评论表中的对应字段
        Query commentQuery = Query.query(Criteria.where("id").is(com.getPublishId()));
        Update update = new Update();
        // 将次数加一
        update.inc("likeCount", 1);
        //设置更新参数   FindAndModifyOptions 选择查询和调整
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(commentQuery, update, options, Comment.class);
        //获取最新的评论数量，并返回
        return andModify.getLikeCount();
    }

    //删除评论点赞数据
    @Override
    public Integer deleteComment(Comment comment) {
        //删除Comment表数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);

        //修改评论表中的总数量
        Query commentQuery = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        //将数量减一
        update.inc("likeCount",-1);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(commentQuery, update, options, Comment.class);
        //获取最新的评论数量，并返回
        return andModify.getLikeCount();
    }

    //列表 喜欢 评论 点赞
    @Override
    public List<Comment> findCommentLove(Integer page, Integer pagesize, Long userId, CommentType love) {
        Query query  =Query.query(Criteria.where("publishUserId").is(userId).and("commentType").is(love.getType())).skip((page-1)*pagesize).limit(pagesize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

}
